﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
using System.Web.UI;

namespace WebInvoicingsys
{
    public class ExportToFile
    {
        public static bool ExportToExcel(DataTable dt, System.Web.HttpResponse Response)
        {
            if (dt == null)
            {
                return false;
            }

            DataGrid dgExcel = new DataGrid();

            string execlName = DateTime.Now.ToString("yyyyMMddHHmmss");
            Encoding encodingType = System.Text.Encoding.UTF8;

            dgExcel.DataMember =dt.TableName;
            dgExcel.DataSource = dt;

            Response.Buffer = true;
            Response.Charset = "utf-8";
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + execlName + ".xls");
            Response.ContentEncoding = encodingType;
            Response.ContentType = "application/ms-excel";
            
            StringWriter oStringWriter = new StringWriter();
            HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
            
            dgExcel.DataBind();
            dgExcel.Visible = true;
            dgExcel.RenderControl(oHtmlTextWriter);
            Response.Write(oStringWriter.ToString());
            Response.Flush();
            Response.Close();
            dgExcel.DataSource = null;
            dgExcel.Visible = false; 

            return true;
        }

        public static bool ExportToExcel(DataTable dt, System.Web.HttpResponse Response, String[] HeadName)
        {
            if (dt == null)
            {
                return false;
            }

            if (HeadName == null)
            {
                return false;
            }

            if (HeadName.Length != dt.Columns.Count)
            {
                return false;
            }


            //重命名表头
            for (int i = 0; i < HeadName.Length; i++)
            {
                dt.Columns[i].ColumnName = HeadName[i].ToString();
            }

            DataGrid dgExcel = new DataGrid();

            string execlName = DateTime.Now.ToString("yyyyMMddHHmmss");
            Encoding encodingType = System.Text.Encoding.UTF8;

            dgExcel.DataMember = dt.TableName;
            dgExcel.DataSource = dt;

            Response.Buffer = true;
            Response.Charset = "utf-8";
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + execlName + ".xls");
            Response.ContentEncoding = encodingType;
            Response.ContentType = "application/ms-excel";

            StringWriter oStringWriter = new StringWriter();
            HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(oStringWriter);

            dgExcel.DataBind();
            dgExcel.Visible = true;
            dgExcel.RenderControl(oHtmlTextWriter);
            Response.Write(oStringWriter.ToString());
            Response.Flush();
            Response.Close();
            dgExcel.DataSource = null;
            dgExcel.Visible = false;

            return true;
        }
    }
}